package Engine.Model
{
	import Engine.Manager.ControlManager;
	import Engine.Manager.EngineManager;
	import Engine.UIControl.Button;
	
	import GameMain.Macro.MacroEvent;
	
	import Interface.Model.ILogPrint;
	import Interface.UIControl.IButton;
	
	import flash.display.Sprite;
	import flash.text.TextField;
	
	import library.Function.GlobalFun;
	
	public class LogPrint extends Sprite implements ILogPrint
	{
		public static const DEBUG:Boolean = false;
		
		private static var _logPrint:LogPrint = null;
		private static const MAX_LOG_LINE:int = 300;
		private var _logText:TextField;
		private var _debugBtn:IButton;
		
		
		public function LogPrint()
		{
			super();
			this.initUI();
		}
		
		private function initUI():void
		{
			this.graphics.lineStyle();
			this.graphics.beginFill(0x000000,0.6);
			this.graphics.drawRect(0, 0, 1000,600);
			this.graphics.endFill();
			
			this._logText = new TextField;
			this._logText.width = 1000;
			this._logText.height = 600;
			this._logText.multiline = true;
			this._logText.wordWrap = true;
			GlobalFun.setTextFormat(this._logText,"SimSun",16,false,0xffffff);
			this._logText.selectable = true;
			this.addChild(this._logText);
			
			var btn:Sprite = new Sprite();
			btn.graphics.lineStyle();
			btn.graphics.beginFill(0xffffff,1);
			btn.graphics.drawRect(0, 0, 50,27);
			btn.graphics.endFill();
			
			var txt:TextField = new TextField;
			txt.multiline = false;
			txt.wordWrap = false;
			txt.text = "调试";
			GlobalFun.setTextFormat(this._logText,"SimSun",16,false,0xffffff);
			txt.selectable = false;
			btn.addChild(txt);
			txt.x = 10;txt.y = 3;
			
			btn.x = 850;
			btn.y = 500;
			
			this._debugBtn = new Button(btn) as IButton;
			this._debugBtn.setOnClickFun(onClickDebug);
			
			this.addChild(btn);
			
			this.visible = false;
		}
		
		public static function instance():LogPrint
		{
			if(_logPrint == null)
				_logPrint = new LogPrint();
			return _logPrint;
		}
		
		public function printLog(...msg):void
		{
			this._printLog(msg.join(" "));
		}
		
		public function showOrHide():void
		{
			this.visible = !this.visible;
		}
		
		private function onClickDebug():void
		{
			EngineManager.eventMgr.throwEvent(MacroEvent.EVENT_DEBUG);
		}
		
		private function _printLog(text:String):void
		{
			EngineManager.eventMgr.throwEvent(MacroEvent.PRINT_LOG,text);
			if(this._logText.text == "")
				this._logText.text = text;
			else
				this._logText.text = this._logText.text + "\r" + text; 
			if (this._logText.maxScrollV >= 200)
			{
				var b:int = this._logText.text.indexOf('\r');
				this._logText.text = this._logText.text.slice(b+1);
			}
			this._logText.scrollV = this._logText.maxScrollV;
		}
		
	}
}